﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using MxWeiXinPF.Common;
using Newtonsoft.Json;

namespace MxWeiXinPF.Web
{
    /// <summary>
    /// token类
    /// </summary>
    public class OAuth_Token
    {
        public OAuth_Token()
        {

        }
        public string _access_token;
        public string _expires_in;
        public string _refresh_token;
        public string _openid;
        public string _scope;
        public string access_token
        {
            set { _access_token = value; }
            get { return _access_token; }
        }
        public string expires_in
        {
            set { _expires_in = value; }
            get { return _expires_in; }
        }

        public string refresh_token
        {
            set { _refresh_token = value; }
            get { return _refresh_token; }
        }
        public string openid
        {
            set { _openid = value; }
            get { return _openid; }
        }
        public string scope
        {
            set { _scope = value; }
            get { return _scope; }
        }

    }

    public class OAuthUser
    {
        public OAuthUser()
        { }
        #region 数据库字段
        private string _openID;
        private string _searchText;
        private string _nickname;
        private int _sex;
        private string _province;
        private string _city;
        private string _country;
        private string _headimgUrl;
        private string _privilege;
        #endregion

        #region 字段属性
        /// <summary>
        /// 用户的唯一标识
        /// </summary>
        public string openid
        {
            set { _openID = value; }
            get { return _openID; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string SearchText
        {
            set { _searchText = value; }
            get { return _searchText; }
        }
        /// <summary>
        /// 用户昵称 
        /// </summary>
        public string nickname
        {
            set { _nickname = value; }
            get { return _nickname; }
        }

        public int sex
        {
            set { _sex = value; }
            get { return _sex; }
        }

        public string province
        {
            set { _province = value; }
            get { return _province; }
        }

        public string city
        {
            set { _city = value; }
            get { return _city; }
        }

        public string country
        {
            set { _country = value; }
            get { return _country; }
        }

        public string headimgurl
        {
            set { _headimgUrl = value; }
            get { return _headimgUrl; }
        }

        public string privilege
        {
            set { _privilege = value; }
            get { return _privilege; }
        }

        private string _unionid;

        public string unionid
        {
            get { return _unionid; }
            set { _unionid = value; }
        }

        #endregion
    }

    public class OAuthClass
    {
        public static OAuth_Token Get_token(string Code, string appid,string appsecret)
        {
            string Str = GetJson("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + appsecret + "&code=" + Code + "&grant_type=authorization_code");

            OAuth_Token Oauth_Token_Model = JsonConvert.DeserializeObject<OAuth_Token>(Str);
            return Oauth_Token_Model;
        }

        public static OAuthUser Get_UserInfo(string REFRESH_TOKEN, string OPENID)
        {
            string Str = GetJson("https://api.weixin.qq.com/sns/userinfo?access_token=" + REFRESH_TOKEN + "&openid=" + OPENID);

            OAuthUser OAuthUser_Model = JsonHelper.ParseFormJson<OAuthUser>(Str);
            return OAuthUser_Model;
        }

        protected static string GetJson(string url)
        {
            System.Net.WebClient wc = new System.Net.WebClient();
            wc.Credentials = CredentialCache.DefaultCredentials;
            wc.Encoding = Encoding.UTF8;
            string returnText = wc.DownloadString(url);

            if (returnText.Contains("errcode"))
            {
                //可能发生错误
            }
            return returnText;
        }


        protected OAuth_Token refresh_token(string REFRESH_TOKEN, string appid)
        {
            string Str = GetJson("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + appid + "&grant_type=refresh_token&refresh_token=" + REFRESH_TOKEN);
            OAuth_Token Oauth_Token_Model = JsonConvert.DeserializeObject<OAuth_Token>(Str);
            return Oauth_Token_Model;
        }
    }
}